package leetcode.problems;

/**
 * Created by Administrator on 2018/3/31.
 */
public class _0330SumofSquareNumbers {
 /**
  * Sum of Square Numbers
  Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
  给定一个非负整数c,判断它是否是两个整数的平方和，即 a*a + b*b = c
  如：
  Input: 5
  Output: True
  注: 1 * 1 + 2 * 2 = 5
  如：
  Input: 3
  Output: False
  */
 public boolean judgeSquareSum(int c) {
  if (c < 0) {
   return false;
  }
  int left = 0, right = (int)Math.sqrt(c);
  while (left <= right) {
   int cur = left * left + right * right;
   if (cur < c) {
    left++;
   } else if (cur > c) {
    right--;
   } else {
    return true;
   }
  }
  return false;
 }
}
